//
//  RegsitViewScorllView.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/7/3.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "RegsitViewScorllView.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation RegsitViewScorllView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self ) {
        [self getUI];
    }
    return self;
}

- (void)getUI
{
    self.isSelected = NO;
    
       
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/4)];
    UIImage *image = [UIImage imageNamed:@"firstPage.png"];
    imageView.image = image;
    //    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
    
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, imageView.frame.origin.y + imageView.frame.size.height + 20, SCREEN_WIDTH/3, 25)];
    phoneLabel.backgroundColor = [UIColor whiteColor];
    phoneLabel.text = @"手机号";
    [self addSubview:phoneLabel];
    
    self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x + phoneLabel.frame.size.width, phoneLabel.frame.origin.y, SCREEN_WIDTH - 20 - phoneLabel.frame.size.width, phoneLabel.frame.size.height)];
    self.nameText.backgroundColor = [UIColor whiteColor];
    self.nameText.clearsOnInsertion = YES;
    self.nameText.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:self.nameText];
    
    UILabel *linePhone = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x, phoneLabel.frame.origin.y + phoneLabel.frame.size.height + 3, SCREEN_WIDTH - 20, 1)];
    linePhone.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    [self addSubview:linePhone];
    
    UILabel *passLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x, linePhone.frame.origin.y + linePhone.frame.size.height + 10, phoneLabel.frame.size.width, phoneLabel.frame.size.height)];
    passLabel.backgroundColor = [UIColor whiteColor];
    passLabel.text = @"密码";
    [self addSubview:passLabel];
    
    self.passText = [[UITextField alloc] initWithFrame:CGRectMake(self.nameText.frame.origin.x, passLabel.frame.origin.y, self.nameText.frame.size.width, passLabel.frame.size.height)];
    self.passText.secureTextEntry = YES;//设置密码密文输入
    self.passText.clearsOnInsertion = YES;
    self.passText.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:self.passText];
    
    UILabel *passLine = [[UILabel alloc] initWithFrame:CGRectMake(passLabel.frame.origin.x, passLabel.frame.origin.y +passLabel.frame.size.height + 3, SCREEN_WIDTH - 20, 1)];
    passLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    [self addSubview:passLine];
    
    UILabel *agianPassLabel = [[UILabel alloc] initWithFrame:CGRectMake(passLabel.frame.origin.x, passLine.frame.origin.y + passLine.frame.size.height + 10, phoneLabel.frame.size.width, phoneLabel.frame.size.height)];
    agianPassLabel.text = @"再次输入密码";
    agianPassLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:agianPassLabel];
    
    self.againPassText = [[UITextField alloc] initWithFrame:CGRectMake(agianPassLabel.frame.origin.x + agianPassLabel.frame.size.width , agianPassLabel.frame.origin.y, SCREEN_WIDTH - 20 - agianPassLabel.frame.size.width, agianPassLabel.frame.size.height)];
    self.againPassText.secureTextEntry = YES;
    self.againPassText.clearsOnInsertion = YES;
    self.againPassText.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:self.againPassText];
    
    UILabel *againLine = [[UILabel alloc] initWithFrame:CGRectMake(agianPassLabel.frame.origin.x, agianPassLabel.frame.origin.y + agianPassLabel.frame.size.height + 3, SCREEN_WIDTH - 20, 1)];
    againLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];    [self addSubview:agianPassLabel];
    [self addSubview:againLine];
    
    
    
    
    UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(agianPassLabel.frame.origin.x, againLine.frame.origin.y + againLine.frame.size.height + 10, agianPassLabel.frame.size.width, agianPassLabel.frame.size.height)];
    codeLabel.backgroundColor = [UIColor whiteColor];
    codeLabel.text = @"手机验证码";
    [self addSubview:codeLabel];
    
    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(codeLabel.frame.origin.x + codeLabel.frame.size.width, codeLabel.frame.origin.y, self.againPassText.frame.size.width, agianPassLabel.frame.size.height)];
    self.textFiled.secureTextEntry = YES;//设置密码密文输入
    //    self.passText.clearsOnInsertion = YES;
    //    self.passText.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:self.textFiled];
    
    UILabel *codeLabelLine = [[UILabel alloc] initWithFrame:CGRectMake(codeLabel.frame.origin.x, codeLabel.frame.origin.y +codeLabel.frame.size.height + 3, SCREEN_WIDTH - 20, 1)];
    codeLabelLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    [self addSubview:codeLabelLine];
    
    
    
    UIButton *textButton = [UIButton buttonWithType:UIButtonTypeCustom];
    textButton.frame = CGRectMake(self.textFiled.frame.size.width - 110, 0, 100, self.textFiled.frame.size.height-5);
    textButton.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:205.0 / 255.0 blue:0 / 255.0 alpha:1];        [textButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [textButton setTitleColor:[UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1] forState:UIControlStateNormal];
    textButton.titleLabel.font = [UIFont systemFontOfSize:15];
    textButton.layer.cornerRadius = CGRectGetHeight([textButton bounds])/2;
    [textButton addTarget:self action:@selector(textButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.textFiled addSubview:textButton];
    
    self.selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.selectButton.frame = CGRectMake(codeLabel.frame.origin.x, codeLabel.frame.origin.y + codeLabel.frame.size.height + 30, 20, 20);
    self.selectButton.backgroundColor = [UIColor whiteColor];
    //边框颜色
    [self.selectButton.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 230/255.0, 230/255.0, 230/255.0, 1});
    [self.selectButton.layer setBorderColor:colorref];
    [self.selectButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.selectButton];
    self.isSelected = NO;
    
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.selectButton.frame.origin.x + self.selectButton.frame.size.width + 5, self.selectButton.frame.origin.y,SCREEN_WIDTH /3, self.selectButton.frame.size.height)];
    detailLabel.text = @"我已阅读并同意";
    detailLabel.backgroundColor = [UIColor whiteColor];
    detailLabel.textColor = [UIColor blackColor];
    detailLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:detailLabel];
    
    UIButton *agreementButton = [UIButton buttonWithType:UIButtonTypeCustom];
    agreementButton.frame = CGRectMake(detailLabel.frame.origin.x + detailLabel.frame.size.width, detailLabel.frame.origin.y, detailLabel.frame.size.width + 15, detailLabel.frame.size.height);
    [agreementButton setTitle:@"《提鲜注册协议》" forState:UIControlStateNormal];
    //下划线
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:@"《提鲜注册协议》"];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    agreementButton.titleLabel.attributedText = content;
    
    [agreementButton setTitleColor:[UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1] forState:UIControlStateNormal];
    agreementButton.backgroundColor = [UIColor whiteColor];
    agreementButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [agreementButton addTarget:self action:@selector(agreementButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:agreementButton];
    
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame = CGRectMake(SCREEN_WIDTH/6, agreementButton.frame.origin.y + agreementButton.frame.size.height + 30, 2 * SCREEN_WIDTH /3, 2 * agreementButton.frame.size.height);
    nextButton.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:205.0 / 255.0 blue:0 / 255.0 alpha:1];
    [nextButton setTitle:@"注  册" forState:UIControlStateNormal];
    nextButton.layer.cornerRadius = CGRectGetHeight([nextButton bounds])/2;
    
    [nextButton setTitleColor:[UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1] forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [nextButton addTarget:self action:@selector(registButtonAction)forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:nextButton];
    
    self.passText.delegate = self;
    self.nameText.delegate = self;
    self.againPassText.delegate = self;
    self.textFiled.delegate = self;
    self.contentSize = CGSizeMake(SCREEN_WIDTH, imageView.frame.size.height + phoneLabel.frame.size.height + passLabel.frame.size.height + agianPassLabel.frame.size.height + codeLabel.frame.size.height + self.selectButton.frame.size.height + nextButton.frame.size.height + 150);

}



- (void)registButtonAction
{
    NSLog(@"注册");
    
    //创建userDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    if ([self.nameText.text isEqualToString:@""] || [self.passText.text isEqualToString:@""] || [self.againPassText.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码或确认密码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 1001;
        [alert show];
    }
    else
    {
        if ( ![self.passText.text isEqualToString:self.againPassText.text]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码输入不相同" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 1002;
            [alert show];
            
        }
        else
        {
            if ([self.passText.text isEqualToString:self.againPassText.text] && self.isSelected == NO) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"请阅读提鲜协议" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功,立即登陆" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [userDefault setObject:self.passText.text forKey:self.nameText.text];
                [userDefault synchronize];
                
                alert.tag = 1003;
                [alert show];
            }
        }
        
    }
    
    
}

// 点击alertView的方法 (签协议)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    if (alertView.tag == 1003) {
        [self.registDelegate registButton];
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.nameText resignFirstResponder];
    [self.passText resignFirstResponder];
    [self.againPassText resignFirstResponder];
    [self.textFiled resignFirstResponder];
    return YES;
}

- (void)textButtonAction
{
    NSLog(@"获取验证码");
    self.textFiled.text = @"123456";
}
- (void)onClick:(UIButton *)button
{
    if (self.isSelected == NO) {
        self.isSelected = YES;
        [button setImage:[UIImage imageNamed:@"regist.png"] forState:UIControlStateNormal];
    }
    else{
        self.isSelected = NO;
        [button setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
        
        
        
    }
    
    
}

- (void)agreementButtonAction

{
    
    //    if (self.isSelected == NO) {
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"请阅读提鲜协议" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    //        [alert show];
    //    }
    //    else
    
    [self.agreementDelegate agreementButton];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
